#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.sympy import *

def dot_prod(u, v):
    return sum(ui * vi for (ui, vi) in zip(u, v))

while True:
    # Entradas
    with display_block('Entradas'):
        func = x * y + randint(1, 4) * z ** 2 / Two
        point = (randint(-4, 4) / Two, randint(-4, 4) / Two, randint(-4, 4) / Two)
        direction = (randint(-4, 4), randint(-4, 4), randint(-4, 4))

    # Resultados auxiliares
    with display_block('Variáveis auxiliares'):
        var_symbs = list(map(var, 'xyzw'))
        var_subs = list(zip(var_symbs, point))
        grad = list(func.diff(x) for x in var_symbs)
        grad = list(df_i.subs(var_subs) for df_i in grad)
        _dir_norm = sqrt(dot_prod(direction, direction))
        normed_direction = list(x / _dir_norm for x in direction)

    # Resultados
    with display_block('Respostas'):
        correct = dot_prod(grad, normed_direction)
        correctNeg = -correct
        nonNormalized = dot_prod(grad, direction)
        nonNormalizedNeg = -nonNormalized
        gradient = sqrt(dot_prod(grad, grad))
        gradientNeg = -gradient
        distractor = dot_prod(grad, (1, 1, 1, 1))
        distractorNeg = -distractor

    if len(set([correct, correctNeg, nonNormalized, nonNormalizedNeg, gradient,
                gradientNeg, distractor, distractorNeg])) == 8:
        break
